package textparser;

import static org.junit.Assert.assertEquals;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.HashSet;
import java.util.Set;

import org.junit.Ignore;
import org.junit.Test;

import com.megadict.format.dict.index.Index;


public class TestSerializingIndex {

    
    @Test
    public void testSerializeIndex() {
//        String indexFilePath = "C:\\test\\av.index";
//        //IndexBuilder builder = new IndexBuilder(indexFilePath);
//        
//        //Set<Index> indexes = builder.build();
//        
//        ObjectOutputStream writer = null;
//        try {
//            writer = new ObjectOutputStream(new FileOutputStream("C:\\test\\indexes.index"));
//        } catch (FileNotFoundException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        } catch (IOException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        
//        try {
//            writer.writeInt(indexes.size());
//            writer.writeObject(indexes);
//        } catch (IOException ioe) {
//            ioe.printStackTrace();
//        } finally {
//            try {
//                writer.close();
//            } catch (IOException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
    }
    
    @Ignore
    public void testDeserializeIndex() {
        String indexFile = "C:\\test\\indexes.index";
        
        Set<Index> indexes = new HashSet<Index>();
        
        ObjectInputStream reader = null;
        try {
            reader = new ObjectInputStream(new FileInputStream(indexFile));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        int totalIndexes = 0;
        Index index = null;
        try {
            totalIndexes = reader.readInt();
            for (int i = 0; i < totalIndexes; i++) {
                index = readIndexBy(reader);
                indexes.add(index);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        
        assertEquals(108857, indexes.size());

    }
    
    private Index readIndexBy(ObjectInputStream reader) throws IOException, ClassNotFoundException {
        return (Index) reader.readObject();
    }
}
